﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Stertex.Service.webservicex.uszip;
using System.Xml.Linq;
using System.Xml;
using System.Text.RegularExpressions;

namespace www.Stertex.com.Controllers
{
    public class ZipCodeController : Controller
    {
        //
        // GET: /ZipCode/

        public ActionResult FindByZip(string ZipCode)
        {
            var city = " ";
            var  state = " ";
            if (!string.IsNullOrEmpty(ZipCode) && ZipCode.Length == 5 && Regex.IsMatch(ZipCode,@"^\d{5}(-\d{4})?$"))
            {
                USZipSoapClient client = new USZipSoapClient();
                var node = client.GetInfoByZIP(ZipCode);
                city = node.FirstChild.ChildNodes[0].InnerText;
                state = node.FirstChild.ChildNodes[1].InnerText;
            }

            return Json(new {City=city, State=state}, JsonRequestBehavior.AllowGet);
        }

    }
}
